<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This file documents the use of the GNU compilers.

Copyright (C) 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>C++ Interface (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="C++ Interface (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="C++ Interface (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Indices.html" rel="index" title="Indices">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="C_002b_002b-Extensions.html" rel="up" title="C++ Extensions">
<link href="Template-Instantiation.html" rel="next" title="Template Instantiation">
<link href="Vague-Linkage.html" rel="prev" title="Vague Linkage">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en_US">
<span id="C_002b_002b-Interface"></span><div class="header">
<p>
Next: <a href="Template-Instantiation.html" accesskey="n" rel="next">Template Instantiation</a>, Previous: <a href="Vague-Linkage.html" accesskey="p" rel="prev">Vague Linkage</a>, Up: <a href="C_002b_002b-Extensions.html" accesskey="u" rel="up">C++ Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="C_002b_002b-Interface-and-Implementation-Pragmas"></span><h3 class="section">7.4 C++ Interface and Implementation Pragmas</h3>

<span id="index-interface-and-implementation-headers_002c-C_002b_002b"></span>
<span id="index-C_002b_002b-interface-and-implementation-headers"></span>
<span id="index-pragmas_002c-interface-and-implementation"></span>

<p><code>#pragma interface</code> and <code>#pragma implementation</code> provide the
user with a way of explicitly directing the compiler to emit entities
with vague linkage (and debugging information) in a particular
translation unit.
</p>
<p><em>Note:</em> These <code>#pragma</code>s have been superceded as of GCC 2.7.2
by COMDAT support and the &ldquo;key method&rdquo; heuristic
mentioned in <a href="Vague-Linkage.html">Vague Linkage</a>.  Using them can actually cause your
program to grow due to unnecessary out-of-line copies of inline
functions.
</p>
<dl compact="compact">
<dd><span id="index-_0023pragma-interface"></span>
</dd>
<dt><code>#pragma interface</code></dt>
<dt><code>#pragma interface &quot;<var>subdir</var>/<var>objects</var>.h&quot;</code></dt>
<dd><p>Use this directive in <em>header files</em> that define object classes, to save
space in most of the object files that use those classes.  Normally,
local copies of certain information (backup copies of inline member
functions, debugging information, and the internal tables that implement
virtual functions) must be kept in each object file that includes class
definitions.  You can use this pragma to avoid such duplication.  When a
header file containing &lsquo;<samp>#pragma interface</samp>&rsquo; is included in a
compilation, this auxiliary information is not generated (unless
the main input source file itself uses &lsquo;<samp>#pragma implementation</samp>&rsquo;).
Instead, the object files contain references to be resolved at link
time.
</p>
<p>The second form of this directive is useful for the case where you have
multiple headers with the same name in different directories.  If you
use this form, you must specify the same string to &lsquo;<samp>#pragma
implementation</samp>&rsquo;.
</p>
<span id="index-_0023pragma-implementation"></span>
</dd>
<dt><code>#pragma implementation</code></dt>
<dt><code>#pragma implementation &quot;<var>objects</var>.h&quot;</code></dt>
<dd><p>Use this pragma in a <em>main input file</em>, when you want full output from
included header files to be generated (and made globally visible).  The
included header file, in turn, should use &lsquo;<samp>#pragma interface</samp>&rsquo;.
Backup copies of inline member functions, debugging information, and the
internal tables used to implement virtual functions are all generated in
implementation files.
</p>
<span id="index-implied-_0023pragma-implementation"></span>
<span id="index-_0023pragma-implementation_002c-implied"></span>
<span id="index-naming-convention_002c-implementation-headers"></span>
<p>If you use &lsquo;<samp>#pragma implementation</samp>&rsquo; with no argument, it applies to
an include file with the same basename<a id="DOCF8" href="#FOOT8"><sup>8</sup></a> as your source
file.  For example, in <samp>allclass.cc</samp>, giving just
&lsquo;<samp>#pragma implementation</samp>&rsquo;
by itself is equivalent to &lsquo;<samp>#pragma implementation &quot;allclass.h&quot;</samp>&rsquo;.
</p>
<p>Use the string argument if you want a single implementation file to
include code from multiple header files.  (You must also use
&lsquo;<samp>#include</samp>&rsquo; to include the header file; &lsquo;<samp>#pragma
implementation</samp>&rsquo; only specifies how to use the file&mdash;it doesn&rsquo;t actually
include it.)
</p>
<p>There is no way to split up the contents of a single header file into
multiple implementation files.
</p></dd>
</dl>

<span id="index-inlining-and-C_002b_002b-pragmas"></span>
<span id="index-C_002b_002b-pragmas_002c-effect-on-inlining"></span>
<span id="index-pragmas-in-C_002b_002b_002c-effect-on-inlining"></span>
<p>&lsquo;<samp>#pragma implementation</samp>&rsquo; and &lsquo;<samp>#pragma interface</samp>&rsquo; also have an
effect on function inlining.
</p>
<p>If you define a class in a header file marked with &lsquo;<samp>#pragma
interface</samp>&rsquo;, the effect on an inline function defined in that class is
similar to an explicit <code>extern</code> declaration&mdash;the compiler emits
no code at all to define an independent version of the function.  Its
definition is used only for inlining with its callers.
</p>
<span id="index-fno_002dimplement_002dinlines-1"></span>
<p>Conversely, when you include the same header file in a main source file
that declares it as &lsquo;<samp>#pragma implementation</samp>&rsquo;, the compiler emits
code for the function itself; this defines a version of the function
that can be found via pointers (or by callers compiled without
inlining).  If all calls to the function can be inlined, you can avoid
emitting the function by compiling with <samp>-fno-implement-inlines</samp>.
If any calls are not inlined, you will get linker errors.
</p>
<div class="footnote">
<hr>
<h4 class="footnotes-heading">Footnotes</h4>

<h5><a id="FOOT8" href="#DOCF8">(8)</a></h3>
<p>A file&rsquo;s <em>basename</em>
is the name stripped of all leading path information and of trailing
suffixes, such as &lsquo;<samp>.h</samp>&rsquo; or &lsquo;<samp>.C</samp>&rsquo; or &lsquo;<samp>.cc</samp>&rsquo;.</p>
</div>
<hr>
<div class="header">
<p>
Next: <a href="Template-Instantiation.html" accesskey="n" rel="next">Template Instantiation</a>, Previous: <a href="Vague-Linkage.html" accesskey="p" rel="prev">Vague Linkage</a>, Up: <a href="C_002b_002b-Extensions.html" accesskey="u" rel="up">C++ Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
